﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;


namespace Team4sCMDB
{
    public partial class User : Form
    {
            
        public bool isAdmin;
        public int gebruikerID,
                    CInr,
                    gebruikersgroepID,
                    PCid;
        public string gebruikersnaam;

        public UserClass userClass;
        private Panel dip;
        private Dictionary<string, TextBox> attributeTextBoxes;
       // public List<User> users;
        
        // Form creation constructor
        public User(bool isAdmin)
        {
            this.isAdmin = isAdmin;
            attributeTextBoxes = new System.Collections.Generic.Dictionary<string, TextBox>();
            dip = new Panel();
            this.Controls.Add(dip);

            InitializeComponent();

            this.Text = "Gebruiker toevoegen of wijzigen";
            userClass = new UserClass("Team4sCmdb.accdb");
            
            // Settings listbox
            listBox1.IntegralHeight = false;

         //  this.users = userClass.getUser();
        }

        // User constructor
        public User(int gebruikerID, int CInr, string gebruikersnaam, int gebruikersgroepID, int PCid)
        {
            this.gebruikerID=gebruikerID;
            this.CInr=CInr;
            this.gebruikersnaam=gebruikersnaam;
            this.gebruikersgroepID=gebruikersgroepID;
            this.PCid = PCid;

            attributeTextBoxes = new System.Collections.Generic.Dictionary<string, TextBox>();
            dip = new Panel();
            this.Controls.Add(dip);

            InitializeComponent();

            this.Text = "Gebruiker toevoegen of wijzigen";
            userClass = new UserClass("Team4sCmdb.accdb");

            // Settings listbox
            listBox1.IntegralHeight = false;

          //  this.users = userClass.getUser();
        }

        public Dictionary<string, TextBox> AttributeTextBoxes
        {
            get { return attributeTextBoxes; }
            set { attributeTextBoxes = value; }
        }

        // Runtime Layout
        private void Locations()
        {
            // tlp loacation update
            tlpListBoxLocation();

            // Dynamic information panel Location
            Point dipLocation = new Point((tlpUserListBox.Location.X + tlpUserListBox.Width + 3), (tlpUserListBox.Location.Y));
            dip.Location = dipLocation;

        }

        // tlpListBoxes location
        private void tlpListBoxLocation()
        {
            tlpUserListBox.Location = new Point(0, 3);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Controls.Remove(dip);
            if (listBox1.SelectedItems.Count == 1)
            {
                User selectedUser = (User)listBox1.SelectedItem;
                try
                {
                    // hier zit nog een error
                    dip = AttributePanelCreator.createPanel(userClass.getSelectedUser(selectedUser.gebruikerID.ToString()), false, null);

                    this.Controls.Add(dip);
                    Locations();
                }
                catch (Exception)
                {
                    MessageBox.Show("dip creation failed");
                }
            }
            else
            {
                // Do nothing
            }
        }

        private void User_Load(object sender, EventArgs e)
        {
            foreach (User u in userClass.getUser())
            {
                listBox1.Items.Add(u);
            }
            listBox1.SelectedIndex = 0;
        }

        private void User_SizeChanged(object sender, System.EventArgs e)
        {
            Locations();
        }


    }
}
